package com.lottery.system.common.interceptor;

import com.lottery.system.common.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author: xiaoxie
 * create: 2024-09-05 20:47
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.common.interceptor
 * description: 登录拦截器
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * @description: 拦截之前的
     * @author: xiaoxie
     * @date: 2024/9/5 20:48
     * @param: [request, response, handler]
     * @return: boolean
     **/

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 获取请求头
        String token = request.getHeader("user_token");
        log.info("获取token：{}", token);
        log.info("获取路径：{}", request.getRequestURI());
        // 令牌解析
        Claims claims = JwtUtil.verifyToken(token);
        if (null == claims) {
            log.error("解析JWT令牌失败！");
            response.setStatus(401);
            return false;
        }
        log.info("解析JWT令牌成功！放行");
        return true;
    }

}